<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\admin\validate\Login as ValidateLogin ;
use think\facade\Session;

class Login extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function login()
    {
        $token = $this->request->token('__token__', 'sha1');
        $this->assign('token', $token);
        return $this->fetch();
    }

    /**
     * 1.token 2.name  3.password  4.code
     */
    public function check()
    {
        // halt(input());
        $validate = new ValidateLogin();
        $data = input();
        // if (password_verify($data['password'], $res[0]['password'])) {
        //     echo "1";
        // }
        // die;
        if (!$validate->check($data)) {

            show(0, $validate->getError(), ['token' => $this->request->token('__token__', 'sha1')]);

        } else {

            // halt(input());
            $res = Db::table('user')->field('password')->where(['user_name' => $data['name']])->select();
            if ($res) {
                //    halt();
                if (password_verify($data['password'], $res[0]['password'])) {
                   show(1,'ok',[]);
                   Session::set('name',$data['name']);
                }else{
                    show(0,'密码错误',[]);
                }

            } else {
                show(0, '用户不存在！');
            }

        }

    }

    //adduser
    public function adduser()
    {
        $data = [
            'user_name' => 'anji',
            'password' => password_hash("anji2020", PASSWORD_DEFAULT),
            'last_login_time' => time(),
            'status' => 1,
            'create_time' => time(),
            'update_time' => time(),

        ];
        Db::table('user')->insert($data);

    }

    public function  del()
    {
        Session::delete('name');
        $this->success('', '/login');
    }

}
